package me.zhengjie.modules.quartz.task;

import cn.hutool.core.date.DateUtil;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.modules.quartz.task.dingdingUtil.DingdingUtil;
import me.zhengjie.modules.quartz.task.dto.PullFailAggregateDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @Author: youqing.yang
 * @Create: 2021/04/22 15:42
 * @Description:
 **/
@Slf4j
@Component
public class PullDataWarningTask {

    @Autowired
    private MongoTemplate mongoTemplate;

    public static final String DINGDING_URL = "https://oapi.dingtalk.com/robot/send?access_token=6058955306dcb1ca1f6ff499cd5726004c66d4d4c1dc2b581cbba17b16ca3d4f";

    public static final String SECRET = "SECc19994f9d0bf0418f484a3408ebae5e3141af34a5760910a08af438e59109859";

    public void sentDingDingMessage(){
        Criteria criteria = Criteria.where("createTime").gte(getDayZero(new Date()));
        Aggregation aggregation = Aggregation.newAggregation(
                Aggregation.match(criteria),
                Aggregation.group("companyUuid","corporationCode")
                        .count().as("count")
                        .first("corporationCode").as("corporationCode")
                        .first("companyUuid").as("companyUuid"));

        AggregationResults<PullFailAggregateDto> results = mongoTemplate.aggregate(aggregation, "sync_data_error_log", PullFailAggregateDto.class);
        List<PullFailAggregateDto> mappedResults = results.getMappedResults();

        if (CollectionUtils.isEmpty(mappedResults)){
            return;
        }
        long corporationNum = mappedResults.stream().map(PullFailAggregateDto::getCorporationCode).distinct().count();
        long companyNum = mappedResults.stream().map(PullFailAggregateDto::getCompanyUuid).distinct().count();

        StringBuffer sb = new StringBuffer();
        String date = DateUtil.format(DateUtil.yesterday(), "YYYY/MM/dd");
        sb.append(date + "\n小程序同步数据异常：");
        sb.append("\n包含集团数：" + corporationNum + "，" + "城市库数量：" + companyNum);
        sb.append("\n请及时查看！");
        DingdingUtil.send(DINGDING_URL, SECRET, sb.toString(), Lists.newArrayList());
    }

    private static Date getDayZero(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }


}
